package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import javafx.application.Application;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;
//暂时关闭初始化
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("-------------------vo初始化开始--------------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            //如果已经有就跳过
            if(strategyStatisVOService.isStrategyVoExist(detail.getId())){
                continue;
            }

            StrategyStatisVO vo = new StrategyStatisVO();

            BeanUtils.copyProperties(detail,vo); //复制 同名属性进去,

            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            vo.setViewnum(detail.getViewnum());
            vo.setReplynum(detail.getReplynum());
            vo.setFavornum(detail.getFavornum());
            vo.setSharenum(detail.getSharenum());
            vo.setThumbsupnum(detail.getThumbsupnum());
            vo.setTitle(detail.getTitle());
            strategyStatisVOService.setStrategyStatisVo(vo);


        }
        System.out.println("-------------vo初始化结束--------------------------------");

        System.out.println("-------------------zset初始化开始--------------------------");

        for (StrategyDetail detail : list) {
            String voId = RedisKeys.STRATGY_STATS_VO.join(detail.getId().toString());
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            if(!strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }


            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVOService.addscoreHot(detail.getId(),score);
        }
        //国内外排行
        for (StrategyDetail detail : list) {
            String voId = RedisKeys.STRATGY_STATS_VO.join(detail.getId().toString());
            String zsetKey = null;
            if (detail.isIsabroad()){
                zsetKey =RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else {
                zsetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }


            if(!strategyStatisVOService.isVoidExsitZset(voId,zsetKey)){
                continue;
            }


            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisVOService.addScoreAbroAndChina(detail.getId(),score);
        }


        System.out.println("-------------zset初始化结束--------------------------------");


    }
}
